package edu.washington.vaccine.ui;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.washington.vaccine.R;
import edu.washington.vaccine.backend.Patient;
import edu.washington.vaccine.backend.VaccineDB;
import edu.washington.vaccine.backend.VaccineLocalHelper;


/*
 * Presents current stored data on a particular patient
 */
public class PatientDataActivity extends Activity {
	@SuppressWarnings("unused")
	private static final String TAG = "PatientDataActivity";
	private Context context;
	Patient p;
	Patient oldP;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.patient_data);
		View root = ((ViewGroup) findViewById(android.R.id.content)).getRootView();
        root.setFocusable(true);
        root.setFocusableInTouchMode(true);
        root.requestFocus();
		Bundle data = getIntent().getExtras();
		p = (Patient) data.getSerializable("patient");
		oldP = (Patient) data.getSerializable("oldPatient");

		this.context = this;

		TextView box = (TextView) findViewById(R.id.textView3);
		box.setText("" + p.id);

		box = (TextView) findViewById(R.id.textView5);
		box.setText(p.fname + " " + p.lname);

		box = (TextView) findViewById(R.id.textView7);
		box.setText(p.dob);

		box = (TextView) findViewById(R.id.textView9);
		box.setText("" + p.location);

		box = (TextView) findViewById(R.id.textView11);
		box.setText(p.mother);

		box = (TextView) findViewById(R.id.textView13);
		box.setText(p.father);

		box = (TextView) findViewById(R.id.textView15);
		final String history = getVaccineHistory(this, "" + p.id);
		box.setText(getVaccineHistory(this, "" + p.id));
//		VaccineDB db = VaccineDB.getInstance();
//		VaccineLocalHelper helper = VaccineLocalHelper.getInstance(this);
//		if (db.isOnlineMode()) {
//			StringBuilder sb = new StringBuilder();
//			Map<String, String> findVacc = new HashMap<String, String>();
//			Set<String[]> vaccines = db.getPatientsVaccines("" + p.id);
//			Cursor c = helper.getReadableDatabase().rawQuery("select vid, vname from Vaccines", new String[]{});
//			while (c.moveToNext()) {
//				findVacc.put("" + c.getInt(0), c.getString(1));
//			}
//			
//			if (vaccines != null) {
//				for (String[] s : vaccines) {
//					String vname = findVacc.get(s[0]);
//					sb.append(vname + ", " + s[1] + "\n");
//				}
//				box.setText(sb.toString());				
//			}
//			
//		} else {
//			box.setText(helper.getPatientsVaccines("" + p.id));
//		}
//		box.setText(VaccineDB.getInstance().getPatientsVaccines("" + p.id));

//		if (p != null)
//			Log.d(TAG, p.dob);

		Button selectVaccine = (Button) findViewById(R.id.give_vaccine_button);
		selectVaccine.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, ChooseVaccineActivity.class);
				i.putExtra("patient", p); 
				i.putExtra("oldPatient", oldP);
				i.putExtra("history", history);
//				VaccineActivity parentActivity = (VaccineActivity) getParent();
//				parentActivity.replaceContentView("givevaccine", i);
				FirstGroup.group.addChildActivity("givevaccine", i);
			}
		});

		Button editButton = (Button) findViewById(R.id.edit_patient_button);
		editButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, EditPatientActivity.class);
				i.putExtra("patient", p); 
//				VaccineActivity parentActivity = (VaccineActivity)getParent();
//				parentActivity.replaceContentView("editpatient", i);
				FirstGroup.group.addChildActivity("editpatient", i);
			}
		});
	}
	
	public static String getVaccineHistory(Context context, String pid) {
		VaccineDB db = VaccineDB.getInstance();
		VaccineLocalHelper helper = VaccineLocalHelper.getInstance(context);
		if (db.isOnlineMode()) {
			StringBuilder sb = new StringBuilder();
			Map<String, String> findVacc = new HashMap<String, String>();
			Set<String[]> vaccines = db.getPatientsVaccines("" + pid);
			Cursor c = helper.getReadableDatabase().rawQuery("select vid, vname from Vaccines", new String[]{});
			while (c.moveToNext()) {
				findVacc.put("" + c.getInt(0), c.getString(1));
			}
			
			if (vaccines != null) {
				for (String[] s : vaccines) {
					String vname = findVacc.get(s[0]);
					sb.append(vname + ", " + s[1] + "\n");
				}
				return sb.toString();
			} else
				return "";
		} else {
			return helper.getPatientsVaccines(pid);
		}
	}

	@Override
	public void onBackPressed() {
//		Intent i;
//		String label;
//		if (oldP == null) {
//			i = new Intent(this, AddPatientActivity.class);
//			label = "register";
//		} else {
//			i = new Intent(this, SearchResultsActivity.class);
//			i.putExtra("patient", oldP);
//			label = "results";
//		}
//		VaccineActivity parentActivity = (VaccineActivity) getParent();
//		parentActivity.replaceContentView(label, i);
		FirstGroup.group.back();
	}
}
